package easy.最长公共前缀;

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        int length = strs[0].length();
        for (int i = 0; i < length; i++) {
            char flag = strs[0].charAt(i);
            for (int j = 1; j < strs.length; j++) {
                //i == strs[j].length() 用于这种情况flower、flow
                if (i == strs[j].length() || flag != strs[j].charAt(i)) {
                    return strs[0].substring(0, i);
                }
            }
        }
        return strs[0];
    }

    public static void main(String[] args) {
        String[] strArray = {"flower", "flow"};
        String[] strs = {"flower", "flow", "flight"};
        Solution solution = new Solution();
        System.out.println(solution.longestCommonPrefix(strArray));
        System.out.println(solution.longestCommonPrefix(strs));
    }
}
